package com.ruoyi.equipment.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.equipment.service.ITEquipmentLogService;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equipment.domain.TEquipmentLog;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备日志Controller
 *
 * @author ruoyi
 * @date 2021-12-23
 */
@RestController
@RequestMapping("/facility/equipmentLog")
public class TEquipmentLogController extends BaseController {
    @Autowired
    private ITEquipmentLogService tEquipmentLogService;

    /**
     * 查询设备日志列表
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEquipmentLog tEquipmentLog) {
        startPage();
        List<TEquipmentLog> list = tEquipmentLogService.selectTEquipmentLogList(tEquipmentLog);
        return getDataTable(list);
    }

    /**
     * 导出设备日志列表
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:export')")
    @Log(title = "设备日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEquipmentLog tEquipmentLog) {
        List<TEquipmentLog> list = tEquipmentLogService.selectTEquipmentLogList(tEquipmentLog);
        ExcelUtil<TEquipmentLog> util = new ExcelUtil<TEquipmentLog>(TEquipmentLog.class);
        util.exportExcel(response, list, "设备日志数据");
    }

    /**
     * 获取设备日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId) {
        return AjaxResult.success(tEquipmentLogService.selectTEquipmentLogByUserId(userId));
    }

    /**
     * 新增设备日志
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:add')")
    @Log(title = "设备日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEquipmentLog tEquipmentLog) {
        return toAjax(tEquipmentLogService.insertTEquipmentLog(tEquipmentLog));
    }

    /**
     * 修改设备日志
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:edit')")
    @Log(title = "设备日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipmentLog tEquipmentLog) {
        return toAjax(tEquipmentLogService.updateTEquipmentLog(tEquipmentLog));
    }

    /**
     * 删除设备日志
     */
    @PreAuthorize("@ss.hasPermi('facility:equipmentLog:remove')")
    @Log(title = "设备日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Integer[] userIds) {
        return toAjax(tEquipmentLogService.deleteTEquipmentLogByUserIds(userIds));
    }

    @GetMapping("/getUserList")
    @Log(title = "获取用户列表", businessType = BusinessType.OTHER)
    public AjaxResult getUserList() {
        Long deptId = getDeptId();
        Long userId = getUserId();
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        sysUser.setUserId(userId);
     return AjaxResult.success(tEquipmentLogService.getUserListByDeptId(sysUser));
    }
}
